﻿using System;
using System.Web;
using BLL;
using Model;

namespace GeniusWebApplication
{
    public partial class download : System.Web.UI.Page
    {
        public static User user = null;
        public static Resource resource = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            user = (User)HttpContext.Current.Session["user"];
            if (user == null)
                Response.Redirect("log.aspx");

            resource = getResource();
            if (resource == null)
                Response.Redirect("index.aspx");

            //判断权限
            //1管理员
            //1.超级管理员
            //2. 普通管理员
            //1.具有资源管理权限
            //2.无该权限
            //2普通用户
            //1 正常 0 锁定 2管理员
            int status = user.status;
            if (status == 0)
            {
                Response.Write("您的账号已被锁定，无法下载!");
                return;
            }
            if (status == 1)
            {
                if (resource.departmentId == 0 || resource.departmentId == user.department.id)
                {
                    new ResourceService().addDownloadTime(resource.id);
                    downloadResource(resource);
                    return;
                }
                else
                {
                    Response.Write("您所在的部门无权下载该资源!");
                    return;
                }
            }
            if (status == 2)
            {
                UserActionService usa = new UserActionService();
                ActionService actionService = new ActionService();
                if (usa.getUserAction(user, 2) != null || resource.departmentId == 0 || resource.departmentId == user.department.id)
                {
                    new ResourceService().addDownloadTime(resource.id);
                    downloadResource(resource);
                    return;
                }
                else
                {
                    Response.Write("您所在的部门或管理组无权下载该资源!");
                    return;
                }
            }
            if (status == 3)
            {
                new ResourceService().addDownloadTime(resource.id);
                downloadResource(resource);
                return;
            }

        }

        //根据id得到Resource
        private Resource getResource()
        {
            string id = Request.QueryString["resourceId"];
            if (id == null)
            {
                Response.Redirect("index.aspx");
                return null;
            }
            else
            {
                int resourceId;
                try
                {
                    resourceId = int.Parse(id);
                }
                catch
                {
                    Response.Redirect("index.aspx");
                    return null;
                }
                ResourceService rs = new ResourceService();
                Resource resource = rs.getResourceById(resourceId);
                return resource;
            }
        }

        //下载文件
        private void downloadResource(Resource resource)
        {
            DownloadHistoryService dhs = new DownloadHistoryService();
            dhs.addDownloadHistory(user, resource.id);
            string filename = urlconvertor(resource.physicalPath);

            if (filename != "")
            {
                string path = Server.MapPath(filename);
                System.IO.FileInfo file = new System.IO.FileInfo(path);
                if (file.Exists)
                {
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + resource.fileName);
                    Response.AddHeader("Content-Length", file.Length.ToString());
                    Response.ContentType = "application/octet-stream";
                    Response.WriteFile(file.FullName);
                    Response.End();
                }
                else
                {
                    Response.Write("该文件不存在，请联系Genius小组管理员！");
                }
            }
        }

        private string urlconvertor(string fileDIr)
        {
            string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
            string fielUrl = fileDIr.Replace(tmpRootDir, ""); //转换成相对路径
            fielUrl = fielUrl.Replace(@"\", @"/");
            return fielUrl;
        }

    }
}